home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: seebs@solutions.solon.com (Peter Seebach)
- Newsgroups: comp.lang.c
- Subject: Re: What is &Variable (declared as: char Variable[10])?
- Date: 28 Feb 1996 20:44:17 -0600
- Organization: Usenet Fact Police (Undercover)
- Message-ID: <4h33u1$31f@solutions.solon.com>
- References: <4gqpa1$3h9@alcor.usc.edu> <4gtab6$acb@ceylon.gte.com> <313318b8.53776146@nntp.ix.netcom.com> <4h1u9d$sqq@ceylon.gte.com>
- NNTP-Posting-Host: solutions.solon.com
-
- In article <4h1u9d$sqq@ceylon.gte.com>, Brenda <g051286> wrote:
- >What is the definition of a pointer? I have always been taught that a
- >pointer is simply an address in memory, and an array name (i.e. myarray)
- >is simply a CONSTANT address.
-
- You have been taught lies.
-
- An array name is merely an identifier; it has no meaning or value outside
- of the context of a program.
-
- "myarray" is a sequence of 7 characters inside quotes.
-
- When you get into a program using that name, you must look at the declaration
- *and the context* to learn what that name refers to.
-
- Consider
- int myarray[10], *myptr;
-
- With these definitions
- sizeof(myarray) == (10 * sizeof(int))
- but
- sizeof(myptr) == (sizeof(int *))
-
- ... They are only the same on the rare machine where (int *) is 10 times as
- large as int. :)
-
- >Of course there are differences between arrays and pointers
- >due to the fact that an array is a CONSTANT address and a pointer is a
- >VARIABLE address.
-
- No, they are due to the fact that the array "myarray" is an object consisting
- of 10 integers, and the pointer "myptr" is an object consisting of one
- pointer.
-
- You are being seriously misled by the fact that "in an expression context,
- a reference to an array decays into a pointer to its first member".
- For instance,
- strcpy(myarray, "foo");
- works by silently passing strcpy the address of myarray[0], rather than
- the actual array. This is because arrays are not really full-fledged
- objects in C, in some ways.
-
- >And the reason I said you shouldn't (note shouldn't
- >not couldn't) say &myarray is:
-
- >==================================================
- >(from "A Book On C", Kelley & Pohl, pg 200)
- >Constructs not to be pointed at:
- >1. Do not point at constants. (&3)
- >2. Do not point at arrays; an array name is a constant. (int a[77]; &a)
- >3. Do not point at ordinary expressions &(k + 99)
- >4. Do not point at register variables. (register v; &v)
- >The address operator can be applied to variables and array elements.
- >===================================================
-
- This is stupid.
- 1, 3, and 4 are all *illegal*. You *cannot* do them.
- 2 is perfectly legal, and is exactly what you may want to do in some cases.
-
- I am much less impressed with "A Book on C" than I used to be.
-
- >So again I say, myarray is DEFINITELY a pointer (i.e. address in memory).
-
- And you are wrong. Please understand; the people you are arguing with
- mostly have 10+ years of experience and study, and have written
- hundreds of programs, libraries, and occasionally just modules. We're
- not stupid.
-
- &myarray is a pointer to an array of 10 ints. myarray is an array of
- 10 ints. In an expression, myarray is *treated like* a pointer to a
- single int.
-
- -s
- --
- Peter Seebach - seebs@solon.com - Copyright 1996 Peter Seebach.
- C/Unix wizard -- C/Unix questions? Send mail for help. No, really!
- FUCK the communications decency act. Goddamned government. [literally.]
- The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.html
-